﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class NguoiDung : Form
    {
        public NguoiDung()
        {
            InitializeComponent();
            radio_QuanLi.Checked = true;
            //GirdView.Rows
            List<DTO_NguoiDung> lnd = new List<DTO_NguoiDung>();
            lnd = BUS_NguoiDung.LayTatCaNguoiDung();
            int i = 0;
            GirdView.Rows.Add(lnd.Count);
            foreach (DTO_NguoiDung nd in lnd)
            {
                GirdView[0, i].Value = nd.MaNguoiDung;
                GirdView[1, i].Value = nd.TenDangNhap;
                GirdView[2, i].Value = nd.CapDo;
                i++;
            }
        }

        private void them_Click(object sender, EventArgs e)
        {
            try
            {

                int temp = 1;
                //string null_value = "";
                if (string.Compare(txt_MaNguoiDung.Text, "") == 0)
                {
                    temp = 0;
                    MessageBox.Show("Thông tin chưa đầy đủ, Chưa nhập Mã Người Dùng!!!");
                }
                if (string.Compare(txt_TenDangNhap.Text, "") == 0)
                {
                    temp = 0;
                    MessageBox.Show("Thông tin chưa đầy đủ, Chưa nhập Tên Đăng Nhập!!!");
                }
                if (string.Compare(txt_MatKhau.Text, "") == 0)
                {
                    temp = 0;
                    MessageBox.Show("Thông tin chưa đầy đủ, Chưa nhập Mật Khẩu!!!");
                }
                if (temp == 1)
                {
                    DTO_NguoiDung nd = new DTO_NguoiDung();
                    nd.MaNguoiDung = txt_MaNguoiDung.Text;
                    nd.TenDangNhap = txt_TenDangNhap.Text;
                    nd.MatKhau = txt_MatKhau.Text;
                    if (radio_QuanLi.Checked)
                    {
                        nd.CapDo = "0";
                    }
                    else
                    {
                        nd.CapDo = "1";
                    }
                    BUS_NguoiDung.ThemNguoiDung(nd);
                    //cap nhap lai thong tin
                    GirdView.Rows.Clear();
                    List<DTO_NguoiDung> lnd = new List<DTO_NguoiDung>();
                    lnd = BUS_NguoiDung.LayTatCaNguoiDung();
                    int i = 0;
                    GirdView.Rows.Add(lnd.Count);
                    foreach (DTO_NguoiDung nda in lnd)
                    {
                        GirdView[0, i].Value = nda.MaNguoiDung;
                        GirdView[1, i].Value = nda.TenDangNhap;
                        GirdView[2, i].Value = nda.CapDo;
                        i++;
                    }
                    txt_MaNguoiDung.Text = "";
                    txt_MatKhau.Text = "";
                    txt_TenDangNhap.Text = "";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void Xoa_Click(object sender, EventArgs e)
        {
            int index = GirdView.SelectedRows[0].Index;
            DTO_NguoiDung nd = new DTO_NguoiDung();
            nd.MaNguoiDung = GirdView[0,index].Value.ToString();
            BUS_NguoiDung.XoaNguoiDung(nd);
            //cap nhap lai thong tin
            GirdView.Rows.Clear();
            List<DTO_NguoiDung> lnd = new List<DTO_NguoiDung>();
            lnd = BUS_NguoiDung.LayTatCaNguoiDung();
            int i = 0;
            GirdView.Rows.Add(lnd.Count);
            foreach (DTO_NguoiDung nda in lnd)
            {
                GirdView[0, i].Value = nda.MaNguoiDung;
                GirdView[1, i].Value = nda.TenDangNhap;
                GirdView[2, i].Value = nda.CapDo;
                i++;
            }
        }
    }
}
